package com.woorigil.social.controller.admin.flower;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.flower.Flower;
import com.woorigil.social.domain.flower.FlowerCondition;
import com.woorigil.social.domain.flower.Tag;
import com.woorigil.social.domain.flower.TagDao;

@Controller
@RequestMapping("/admin/koreaflower")
public class TagController extends BaseController {
	@Autowired TagDao tagDao;
	
	@RequestMapping("/tag/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		Tag tag = tagDao.select(id);
		ModelAndView mav = new ModelAndView(getForwordPage("admin/koreaflower/tag/form"));
		mav.addObject("tag", tag);
		return mav;
	}

	@RequestMapping("/tag/insert")
	public ModelAndView insert(Tag tag, HttpServletRequest request) throws Exception {
		int id = tagDao.insert(tag);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/admin/koreaflower/tag/list"));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/tag/update")
	public ModelAndView update(Tag tag) throws Exception {
		tagDao.update(tag);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/admin/koreaflower/tag/list"));
		mav.addObject("id", tag.getId());
		return mav;
	}
	
	@RequestMapping("/tag/delete")
	public ModelAndView delete(int id) {
		tagDao.delete(id);
		ModelAndView mav = new ModelAndView(getRedirectUrl("/admin/koreaflower/tag/list"));
		mav.addObject("id", id);
		return mav;
	}
	
//	@RequestMapping("/view")
//	public ModelAndView view(int id) {
//		Flower flower = tagDao.select(id);
//		if (flower == null) {
//			throw new IllegalArgumentException("message");
//		}
//		ModelAndView mav = new ModelAndView("flower/view");
//		mav.addObject("flower", flower);
//		return mav;
//	}	
//	
	@RequestMapping("/tag/list")
	public ModelAndView list() {
		List<Tag> tagList = tagDao.search();
		
		ModelAndView mav = new ModelAndView(getForwordPage("admin/koreaflower/tag/list"));
		mav.addObject("tagList", tagList);
		return mav;
	}
}